/*
 * Copyright 2010 Alexey Romensky
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

/*
 * User: Alexey Romensky
 * Date: 20.02.2010
 * Time: 0:17:42
 */
package com.googlecode.gwt.logging.sample.client;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.RootPanel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.extjs.gxt.ui.client.Style.HorizontalAlignment.LEFT;
import static com.extjs.gxt.ui.client.Style.VerticalAlignment.TOP;


public class Logging implements EntryPoint {

    private static final Logger log = LoggerFactory.getLogger(Logging.class);

    public void onModuleLoad() {

        GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {

            @Override
            public void onUncaughtException(Throwable e) {
                log.error("Uncaught Exception:", e);
            }
        });

        setErrorHandler();

        DeferredCommand.addCommand(new Command() {

            @Override
            public void execute() {

                final Button button = new Button("Append", new SelectionListener<ButtonEvent>() {

                    private int i;

                    @Override
                    public void componentSelected(ButtonEvent ce) {

                        try {
                            throw new Exception("hello");
                        } catch (Exception e) {
                            log.error("Hello, World! " + i++, e);
                        }
                    }
                });

                final Viewport viewport = new Viewport();
                viewport.setLayout(new TableLayout());
                viewport.add(button, new TableData(LEFT, TOP));
                RootPanel.get().add(viewport);
            }
        });
    }

    private native void setErrorHandler() /*-{

        if ($wnd != window) {
            window.onerror = @com.googlecode.gwt.logging.sample.client.Logging::handleOnError(Ljava/lang/String;Ljava/lang/String;I);
        }

        var oldOnError = $wnd.onerror;
        $wnd.onerror = function(msg, url, line) {

            var result, oldResult;
            try {
                result = @com.googlecode.gwt.logging.sample.client.Logging::handleOnError(Ljava/lang/String;Ljava/lang/String;I)(msg, url, line);
            } finally {
                oldResult = oldOnError && oldOnError(msg, url, line);
            }

            if (result != null) return result;
            if (oldResult != null) return oldResult;
        }
    }-*/;

    private static boolean handleOnError(String msg, String url, int line) {

        log.error("Uncaught JavaScript exception [" + msg + "] in " + url + ", line " + line);
        return true;
    }
}
